package com.ita.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

public class CharFilter implements Filter {

	private String charset;
	public static Logger logger=Logger.getLogger("X");

	@Override
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		String charset = config.getInitParameter("chars");
		if (charset != null && !charset.isEmpty()) {
			this.charset = charset;
		}
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		System.out.println("char filter");
		HttpServletRequest req = (HttpServletRequest) request;
		if (req.getMethod().equalsIgnoreCase("GET")) {
			response.setCharacterEncoding(charset);
		} else {
			req.setCharacterEncoding(charset);//
		}
		chain.doFilter(req, response);

	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

}
